﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using APA_DAO;
using TAM_SPORT;

namespace AMS
{
    public partial class FrmDanhSachPhieuThu : Form
    {
        public FrmDanhSachPhieuThu()
        {
            InitializeComponent();
            txtStartDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtEndDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            loadGrid();
            dtResult.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSkyBlue;
            dtResult.GridColor = Color.BurlyWood;
            dtResult.EnableHeadersVisualStyles = false;
            this.BackColor = Color.LightCyan;
        }

        private void thayĐổiThôngTinNgườiThuêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmEditMembers f = new FrmEditMembers();
            f.ShowDialog();
            f.Dispose();
        }

        private void thayĐổiThôngTinXeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmEditMoto f = new FrmEditMoto();
            f.ShowDialog();
            f.Dispose();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadGrid();
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private DataGridViewImageCell loadImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                DataGridViewImageCell imageCell = new DataGridViewImageCell();
                Image imagedis = resizeImage(image, new Size(150, 150));
                imageCell.Value = imagedis;
                image.Dispose();
                return imageCell;
            }
            catch
            {
                return new DataGridViewImageCell();
            }
        }

        private bool validate()
        {
            bool errFlg = false;
            StringBuilder strBuilder = new StringBuilder();
            int i = 0;
            DateTime day;
            if (!DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day))
            {
                i++;
                strBuilder.Append(i.ToString() + ". Ngày chi tiêu (bắt đầu) sai định dạng ngày/tháng/năm.");
                errFlg = true;
            }

            if (!DateTime.TryParseExact(txtEndDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out day))
            {
                i++;
                if (errFlg)
                {
                    strBuilder.Append("\n");
                }
                strBuilder.Append(i.ToString() + ". Ngày chi tiêu (kết thúc) sai định dạng ngày/tháng/năm.");
                errFlg = true;
            }

            if (!string.IsNullOrEmpty(strBuilder.ToString()))
            {
                Error er = new Error(strBuilder.ToString());
                er.ShowDialog();
                er.Dispose();
            }
            return !errFlg;
        }

        private void loadGrid()
        {
            if (validate())
            {
                DanhSachPhieuThuDao dao = new DanhSachPhieuThuDao();
                DataTable table = dao.getDanhSachPhieuThu(txtSearch.Text,
                    string.IsNullOrEmpty(txtStartDate.Text) ? default(DateTime?) : DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None),
                    string.IsNullOrEmpty(txtEndDate.Text) ? default(DateTime?) : DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None));
                int i = 0;
                dtResult.Rows.Clear();
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        i = dtResult.Rows.Add();
                        dtResult.Rows[i].Cells[0].Value = (i + 1).ToString();
                        dtResult.Rows[i].Cells[1] = row["HINH_ANH"] == null ? null : loadImage(row["HINH_ANH"].ToString());
                        dtResult.Rows[i].Cells[2].Value = row["HO_VA_TEN"].ToString();
                        dtResult.Rows[i].Cells[3].Value = row["SO_PHONG"].ToString();
                        dtResult.Rows[i].Cells[4].Value = DateTime.Parse(row["NGAY_DONG"].ToString()).ToString("dd/MM/yyyy");
                        //dtResult.Rows[i].Cells[5].Value = DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()).ToString("dd/MM/yyyy");
                        dtResult.Rows[i].Cells[5].Value = row["TIEN_NUOC"].ToString();
                        dtResult.Rows[i].Cells[6].Value = row["TIEN_DIEN"].ToString();
                        dtResult.Rows[i].Cells[7].Value = row["TIEN_PHONG"].ToString();
                        dtResult.Rows[i].Cells[8].Value = row["CHI_PHI_KHAC"].ToString();
                        dtResult.Rows[i].Cells[10].Value = row["SO_THUE_PHONG"].ToString();
                        dtResult.Rows[i].Cells[11].Value = row["THU_TIEN_ID"].ToString();
                        dtResult.Rows[i].Cells[12].Value = row["MAX_ID"].ToString();
                        //if (row["NGAY_KET_THUC_KY_NAY"].GetType().FullName == "System.DBNull" || DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) <= System.DateTime.Now.AddDays(3))
                        //{
                        //    dtResult.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        //}
                        //if (row["NGAY_KET_THUC_KY_NAY"].GetType().FullName != "System.DBNull" && System.DateTime.Now.AddDays(3) < DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) && DateTime.Parse(row["NGAY_KET_THUC_KY_NAY"].ToString()) <= System.DateTime.Now.AddDays(5))
                        //{
                        //    dtResult.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                        //}
                    }
                }
            }
        }

        private void dtResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dtDelete_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int thuTienId = int.Parse(dtResult.Rows[e.RowIndex].Cells[11].Value.ToString());
                if (e.ColumnIndex == 9)
                {
                    if (thuTienId == int.Parse(dtResult.Rows[e.RowIndex].Cells[12].Value.ToString()))
                    {
                        Confirm f = new Confirm("Bạn muốn xóa thông tin phiếu thu này?");
                        f.ShowDialog();
                        if (f.IsAccept)
                        {
                            DanhSachPhieuThuDao dao = new DanhSachPhieuThuDao();
                            if (dao.deletePhieuThu(thuTienId))
                            {
                                Result re = new Result("Xóa thành công.");
                                re.ShowDialog();
                                re.Dispose();
                                loadGrid();
                            }
                            else
                            {
                                Error er = new Error("Xóa thất bại.");
                                er.ShowDialog();
                                er.Dispose();
                            }
                            f.Dispose();
                        }
                    }
                    else 
                    {
                        Error er = new Error("Không thể xóa phiếu thu này.");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
            }
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            txtStartDate.Text = dtpStartDate.Value.ToString("dd/MM/yyyy");
        }

        private void dtpEndDate_ValueChanged(object sender, EventArgs e)
        {
            txtEndDate.Text = dtpEndDate.Value.ToString("dd/MM/yyyy");
        }
    }
}
